package com.situ.springboot.lambda;

import com.situ.springboot.entity.Student;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

public class LambdaDemo {
    public static void main(String[] args) {
        // 匿名内部类
        // 1、定义一个没有名字的类
        // 2、这个类实现了Runnable接口
        // 3、创建了这个类的对象
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("LambdaDemo.run1");
            }
        }).start();

        // Lambda表达式的写法
        // (参数列表) -> {
        //    方法体
        // }
        new Thread(()->{
            System.out.println("LambdaDemo.run2");
        }).start();

        new Thread(() -> System.out.println("LambdaDemo.run3")).start();
    }

    @Test
    public void test2() {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student(1, "张三", 34, "男"));
        list.add(new Student(1, "李四", 45, "男"));
        list.add(new Student(1, "王五", 87, "男"));
        list.add(new Student(1, "赵六", 22, "男"));
        // Comparable Comparator
        /*Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });*/

        /*Collections.sort(list, (Student o1, Student o2)->{
            return o1.getAge() - o2.getAge();
        } );*/

        Collections.sort(list, (o1,o2) -> o1.getAge() - o2.getAge());

        for (Student student : list) {
            System.out.println(student);
        }
    }
}
